<?php
include "./include/om_config.inc";
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $smarty = new SmartyWWW();
  $fromDate = isset($_POST['fromDateYear'])? $_POST['fromDateYear']."-".strtoupper($_POST['fromDateMonth'])."-".$_POST['fromDateDay']:mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
  $toDate = isset($_POST['toDateYear'])? $_POST['toDateYear']."-".strtoupper($_POST['toDateMonth'])."-".$_POST['toDateDay']:mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
  $smarty->assign("fromDate",$fromDate);
  $smarty->assign("toDate",$toDate);
  
  $displayItemName =0;
  if(isset($_POST['itemId']) && $_POST['itemId'] == '0')
  {
  	$displayItemName = '1';
  }
  $smarty->assign("displayItemName",$displayItemName);
  /////For Display in table ////
  
         
	$weekDay               = array();
	$expiryDateBc          = array();
	$itemName              = array();
	$bhavcopyDate          = array();
	$openPrice             = array();
	$highPrice             = array();
	$lowPrice              = array();
	$closePrice            = array();
	$openInterest          = array();
	$openInterestInLots    = array();
	$openInterestDisp      = array();
	$displayCurrentRow     = array();
	$lotSize               = array();
	$openInterestTot       = array();
	$openInterestInLotsTot = array();
	$exchangeValue         = array();
	$highestHighPrice      = 0 ;
	$lowestLowPrice        = 999999;
	
	//selectitem:Start
	$itemQuery = "SELECT itemId,min 
	                FROM item";
	$itemQueryResult = mysql_query($itemQuery);
	$count=0;
	while($itemQueryRow = mysql_fetch_array($itemQueryResult))
	 $itemMinArray[$itemQueryRow['itemId']] = $itemQueryRow['min'];
	//selectitem:End
	
	$selectQuery = "SELECT exchange,contractCode,expiryDate FROM bhavcopyncdx ORDER BY bhavcopyDate,expiryDate";
	$resultQuery = mysql_query($selectQuery);
	$count=0;
	while($rowQuery = mysql_fetch_array($resultQuery))
	{
	 $contract[$count] = $rowQuery['contractCode'];
	 $expiry[$count]   = $rowQuery['expiryDate'];
	 $exchange[$count] = $rowQuery['exchange'];
	 $count++;
	}
	
	$sortBy =isset($_POST['sortBy'])? $_POST['sortBy'] :0;
	$currentItemId =isset($_POST['itemId'])? $_POST['itemId'] :0;
	$expDate = isset($_POST['expiryDate'])? $_POST['expiryDate'] : 0; 
	$currentExchange = isset($_POST['exchange'])? $_POST['exchange'] : 0; 
	$displayCount = 0;  
	$displayQuery = "SELECT * FROM bhavcopyncdx WHERE bhavcopyDate >= '".$fromDate."' AND bhavcopyDate <= '".$toDate."'";
	if($currentItemId > '0')									
	{
    $displayQuery .= " AND contractCode='".$currentItemId."' ";
	
	 if($currentExchange !='BSE CASH' && $expDate != 0)
	 { 
	   $displayQuery .= " AND expiryDate='".$expDate."'"; 
	 }
	}
	$displayQuery .="  AND exchange='".$currentExchange."'";
	if($sortBy == "Item Name")
	$displayQuery .="  ORDER BY contractCode,expiryDate,bhavcopyDate";
	else   
	 $displayQuery .="  ORDER BY bhavcopyDate,contractCode,expiryDate";
	$displayResult = mysql_query($displayQuery);
	$currentTotal  = 0;
	$currentTotalInLots  = 0;
	$displayCountCurrentRow = 0;
	while($displayRow = mysql_fetch_array($displayResult))
	{   
	$weekDay[$displayCount]      = date("l", mktime(0,0,0,substr($displayRow['bhavcopyDate'],5,2),substr($displayRow['bhavcopyDate'],8,2),substr($displayRow['bhavcopyDate'],0,4)));
	$bhavcopyDateYYYYMMDD[$displayCount] = $displayRow['bhavcopyDate'];
	$bhavcopyDate[$displayCount] = mysqlToDDMMYY($displayRow['bhavcopyDate']);
  $itemName[$displayCount] 		 = $displayRow['contractCode'];
  $expiryDateBc[$displayCount] = $displayRow['expiryDateBc'];
  $openPrice[$displayCount] 	 = $displayRow['openPrice'];
  $highPrice[$displayCount]  	 = $displayRow['highPrice'];
  $lowPrice[$displayCount] 		 = $displayRow['lowPrice'];
  $closePrice[$displayCount]   = $displayRow['closePrice'];
  $openInterest[$displayCount] = $displayRow['openInterest'];
  $currentItemMin = (isset($itemMinArray[$displayRow['contractCode']]) && $itemMinArray[$displayRow['contractCode']] != 0) ? $itemMinArray[$displayRow['contractCode']] : 1;
  $openInterestInLots[$displayCount] = $displayRow['openInterest'] / $currentItemMin;
  
  if($displayCount > 0 && $itemName[$displayCount - 1] == $displayRow['contractCode'] 
           && $bhavcopyDateYYYYMMDD[$displayCount - 1] == $displayRow['bhavcopyDate'])
    $displayCurrentRow[$displayCount] = "N";
  else
  {
    $displayCurrentRow[$displayCount] = "Y";
    $displayCountCurrentRow           = $displayCount;
    
    //To find the Highest Price and Lowest Price form data : Starts
	  if($highestHighPrice < $highPrice[$displayCount])
	    $highestHighPrice = $highPrice[$displayCount];
	    
	  if($lowestLowPrice > $lowPrice[$displayCount] && $lowPrice[$displayCount] != 0)
	    $lowestLowPrice = $lowPrice[$displayCount];
	  //To find the Highest Price and Lowest Price form data : Ends
  }
    
  if($displayCount > 0 && $itemName[$displayCount - 1] == $itemName[$displayCount] 
           && $bhavcopyDateYYYYMMDD[$displayCount - 1] == $displayRow['bhavcopyDate'])
    $openInterestDisp[$displayCount] = "Y";
  else
  {
    $currentTotal  = 0;
    $currentTotalInLots  = 0;
    $openInterestDisp[$displayCount] = "N";
  }
  $currentTotal  += $displayRow['openInterest'];
  $currentTotalInLots  += $displayRow['openInterest'] / $currentItemMin;
  $lotSize[$displayCountCurrentRow]               = $currentItemMin;
  $openInterestTot[$displayCountCurrentRow]  			= $currentTotal;
  $openInterestInLotsTot[$displayCountCurrentRow]	= $currentTotalInLots;
  
  $displayCount++;
   }
  
  ////Request parameters, if passed : transfer to proper variable :End
  
  //Item records :Start
  $itemIdSelected   = $currentItemId;
  $itemIdValues     = array();
  $itemIdOptions    = array();
  $itemCount        = 0;
  $itemIdValues[0]  = '0';
  $itemIdOptions[0] = "All";
  $itemRecords      = array();
  $itemCount++;
  $selectedExchange = isset($_POST['exchange'])? $_POST['exchange'] :'NCDX';
  $smarty->assign("selectedExchange",$selectedExchange);
  
  $itemQuery = "SELECT DISTINCT(contractCode) FROM bhavcopyncdx WHERE exchange='".$selectedExchange."' ORDER BY contractCode";
  $itemResult = mysql_query($itemQuery);
  while($itemRow = mysql_fetch_array($itemResult))
  {
    $itemIdValues[$itemCount]  = $itemRow['contractCode'];
    $itemIdOptions[$itemCount] = $itemRow['contractCode'];
    $itemCount++;
  }
  
  //Expiry records :Start
  if(isset($_POST['expiryDate']))
  {
    if($_POST['itemIdChanged']==1 || $currentItemId=="All")
      $expiryDateSelected = 0;
    else
      $expiryDateSelected = $_POST['expiryDate'];
  }
  else
    $expiryDateSelected = 0;
    
  $expiryDateValues = array();
  $expiryDateOptions = array();
  
  $i=0;
  $expiryDateValues[0]  = 'All';
  $expiryDateOptions[0] = 'All';
  $i++;
    
  if($currentItemId!="All")
  {
    $expiryQuery = "SELECT DISTINCT(expiryDate),expiryDateBc FROM bhavcopyncdx 
                    WHERE contractCode='".$currentItemId."' ORDER BY expiryDate";
    $expiryResult = mysql_query($expiryQuery);
    while($expiryRow = mysql_fetch_array($expiryResult))
    {
        $expiryDateValues[$i]  = $expiryRow['expiryDate'];
        $expiryDateOptions[$i] = $expiryRow['expiryDateBc'];
        $i++;
    }
  }
  if(isset($_POST['avgLow']) && isset($_POST['avgHigh']))
  {	
    $avgHigh=$_POST['avgHigh'];
    $avgLow=$_POST['avgLow'];
  }
  
  $selectExchange="SELECT DISTINCT (exchange) FROM bhavcopyncdx";
  $resultExchange = mysql_query($selectExchange);    
  $j=0;
  while($rowExchange = mysql_fetch_array($resultExchange))
  {
  	$exchangeValue[$j] = $rowExchange['exchange'];
  	$j++;
  }
   
  //////End for display in table //////////

	////Sort By Array : Start
	$sortingSelected  = isset($_POST['sortBy']) ? $_POST['sortBy'] : 0;

//	$sortValuesArr[0] = "Item Name";
//	$sortOutputArr[0] = "Item Name";
	$sortValuesArr[0] = "Date";
	$sortOutputArr[0] = "Date";	
	
	////Sort By Array : End  
  
  $avgHigh = isset($avgHigh)? $avgHigh : "";

  $smarty->assign("sortValuesArr",$sortValuesArr);
  $smarty->assign("sortOutputArr",$sortOutputArr);
  $smarty->assign("sortingSelected",$sortingSelected);
  $smarty->assign("weekDay",$weekDay);
  $smarty->assign("itemName",$itemName);
  $smarty->assign("expiryDateBc",$expiryDateBc);
  $smarty->assign("bhavcopyDate",$bhavcopyDate);
  $smarty->assign("openPrice",$openPrice);
  $smarty->assign("highPrice",$highPrice);
  $smarty->assign("lowPrice",$lowPrice);
  $smarty->assign("closePrice",$closePrice);
  $smarty->assign("openInterest",$openInterest);
  $smarty->assign("openInterestInLots",$openInterestInLots);
  $smarty->assign("openInterestDisp",$openInterestDisp);
  $smarty->assign("displayCurrentRow",$displayCurrentRow);
  $smarty->assign("lotSize",$lotSize);
  $smarty->assign("openInterestTot",$openInterestTot);
  $smarty->assign("openInterestInLotsTot",$openInterestInLotsTot);
  
  $smarty->assign("itemIdSelected",$itemIdSelected);
  $smarty->assign("itemIdValues",$itemIdValues);
  $smarty->assign("itemIdOptions",$itemIdOptions);
  $smarty->assign("expiryDateSelected",$expiryDateSelected);
  $smarty->assign("expiryDateValues",$expiryDateValues);
  $smarty->assign("expiryDateOptions",$expiryDateOptions);
  $smarty->assign("exchangeValue",$exchangeValue);
  $smarty->assign("highestHighPrice",$highestHighPrice);
  $smarty->assign("lowestLowPrice",$lowestLowPrice);
  
  $smarty->display('bhavCopyListNcdx.tpl');
}
?>